//
//  PrivacyPromptBoxView.m
//  alotOf
//
//  Created by Mac_Black_Mini on 2025/9/2.
//

#import "PrivacyPromptBoxView.h"
#import "Masonry.h"

@interface PrivacyPromptBoxView()
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *contentView;
@end

NS_ASSUME_NONNULL_BEGIN

@implementation PrivacyPromptBoxView
+ (void)showOnceWithTitle:(NSString *)title message:(NSString *)message inViewController:(UIViewController *)viewController storageKey:(NSString *)storageKey{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults boolForKey:storageKey]) {
        return;
    }
    
    PrivacyPromptBoxView *box = [[PrivacyPromptBoxView alloc] initWithFrame:viewController.view.bounds];
    box.backgroundColor = [UIColor clearColor];
    
    // 背景遮罩
    box.backgroundView = [[UIView alloc] initWithFrame:box.bounds];
    box.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [box addSubview:box.backgroundView];
    
    // 内容视图
    box.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    box.contentView.center = box.center;
    box.contentView.backgroundColor = [UIColor whiteColor];
    box.contentView.layer.cornerRadius = 10;
    [box addSubview:box.contentView];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 260, 60)];
    titleLabel.text = title;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont boldSystemFontOfSize:15];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [box.contentView addSubview:titleLabel];
    
    // 消息
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 260, 250)];
    messageLabel.text = message;
    messageLabel.numberOfLines = 0;
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.font=[UIFont systemFontOfSize:10];
    [box.contentView addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(box.contentView).offset(15);
        make.centerX.equalTo(box.contentView);
        make.height.mas_equalTo(250);
        make.width.mas_equalTo(260);
    }];
    // 确定按钮
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
    confirmButton.frame = CGRectMake(50, 120, 100, 40);
    [confirmButton setTitle:@"agree and continue" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [confirmButton addTarget:box action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
    [box.contentView addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(box.contentView).offset(5);
        make.bottom.equalTo(box.contentView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(150, 40));
    }];
    // 确定按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(200, 150, 100, 40);
    [cancelButton setTitle:@"Decline and Exit" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [cancelButton addTarget:box action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
    [box.contentView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(box.contentView).offset(-5);
        make.bottom.equalTo(box.contentView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(150, 40));
    }];
    // 显示
    [viewController.view addSubview:box];
    
    // 标记为已显示
    [defaults setBool:YES forKey:storageKey];
    [defaults synchronize];
}
- (void)dismiss:(id)sender {
    [self removeFromSuperview];
}

@end

NS_ASSUME_NONNULL_END
